package com.ismartv.sale.control;

import com.ismartv.sale.bean.UserInfo;
import com.ismartv.sale.dao.CommonDao;
import com.ismartv.sale.dao.DaoException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MainDispatchServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/json;charset=utf-8");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = req.getParameter("userName");
		String password = req.getParameter("password");

		CommonDao dao = null;
		UserInfo user = null;
		try {
			dao = new CommonDao();
		} catch (DaoException e) {
			e.printStackTrace();
		}
		if (dao != null) {
			user = dao.getUserInfo(name, password);
		}
		if (user != null) {
			req.setAttribute("userInfo", user);
			int privilegeCode = user.getPrivilegeCode();
			switch (privilegeCode) {
			case 100:
				dispatchAction(resp, "");
				break;
			case 200:
				dispatchAction(resp, "");
				break;
			case 300:
				dispatchAction(resp, "");
				break;
			}
		}
	}

	private void dispatchAction(HttpServletResponse resp, String action)
			throws ServletException, IOException {
		resp.sendRedirect(action);
	}
}
